<?xml version="1.0" encoding="UTF-8" ?>
<%@ page
	contentType="text/html;charset=UTF-8"
	pageEncoding="UTF-8"
	import="fr.utbm.gi.in56.vehicle.VehicleManager"
	import="java.util.List"
	import="java.util.HashMap"
	import="fr.utbm.gi.in56.vehicle.BrandEntry"
	import="fr.utbm.gi.in56.vehicle.ModelEntry"
	import="fr.utbm.gi.in56.error.ErrorType"
%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <link rel="stylesheet" media="screen" type="text/css" title="Style" href="style.css" />
        <script type="text/javascript" src="ressources/scripts/modelList.js"></script>
        <title>L'Entre-Pot</title>
    </head>
    <body>
        <div id ="container">
            <jsp:directive.include file="header.jsp"/>
            <jsp:directive.include file="topMenu.jsp"/>
            <div id="core">
            	<h2 align="center">Publier une annonce de vente</h2>
            	<div id="defaultForm">
            		<% ErrorType error = (ErrorType)request.getAttribute( "postAdError" );%>
					<%
						if( error != null )
						{
					%>
							<div id="error">
							    <%= error.getErrorMessage() %>
							</div>
					<%
						}
					%>
            		<form method="post" action="PostSellAdServlet" enctype="multipart/form-data" accept-charset="utf-8 name="connexion">
						<div id="defaultFormEntry">
							<label for="title">Titre de l'annonce :</label>
							<input type="text" name="title" id="title" maxlength="50"/>
						</div>
						<%
						List< BrandEntry > brandList =
							( List< BrandEntry > )request.getAttribute("brandList" );
						List< List< ModelEntry > > modelLists =
							( List< List< ModelEntry > > )request.getAttribute("modelLists" );
						
						if( ( brandList != null ) && ( modelLists != null ) )
						{
							int brandNb = brandList.size();
						%>
							<script>
							var modelIds = new Array(<% out.print( brandNb ); %> );
							var modelNames = new Array(<% out.print( brandNb ); %> );
							<%				
								for ( int i = 0; i < brandNb; ++i )
								{
									List< ModelEntry > brandModels = modelLists.get( i );
									int modelsNb = brandModels.size();
									
									out.print( "modelIds[" + i + "] = new Array(" + modelsNb + ");" );
									out.print( "modelNames[" + i + "] = new Array(" + modelsNb + ");" );
									
									for ( int j = 0; j < modelsNb; ++j )
									{
										out.print( "modelIds[" + i + "][" + j + "] = '" + brandModels.get( j ).getId() + "';" );
										out.print( "modelNames[" + i + "][" + j + "] = '" + brandModels.get( j ).getModel() + "';" );
									}
								}
							%>
							</script>
						<%
						}
						%>
						
						<div id="defaultFormEntry">   		
							<label for="brand">Marque :</label>
							<select name="brand" id="brand" onchange="showCarList('brand', 'model', 'Choisissez');">
								<option value="0">Choisissez</option>
								<%
									if ( brandList != null )
									{
										for ( BrandEntry brand : brandList )
										{
								%>
											<option value="<%= brand.getId() %>"><%= brand.getBrand() %></option>
								<%				
										}
									}
								%>
							</select>
						</div>
						<div id="defaultFormEntry">
							<label for="model">Modèle :</label>
							<select name="model" id="model" disabled="disabled">
								<script language="JavaScript" type="text/JavaScript"> 
									showCarList('brand', 'model', 'Choisissez');
								</script>
							</select>
						</div>
						<div id="defaultFormEntry">	
							<label for="vehicle_date">Année :</label>
							<select name="vehicle_date" id="vehicle_date">
							<option value="0" selected="selected">Choisissez</option>
							<%
							for ( int year = 2011; year >=1945 ; --year )
							{
							%>
							<option value="<%= year %>"><%= year %></option>
							<%
							}
							%>
							</select>
						</div>
						<div id="defaultFormEntry">
							<label for="km_nb">Kilométrage :</label>
							<input type="text" name="km_nb" id="km_nb" />
						</div>
						<%
						HashMap< Integer, String > energyMap =
							(HashMap< Integer, String >)request.getAttribute("energiesList" );
						%>
						<div id="defaultFormEntry">
							<label for="energy">Energie :</label>
							<select name="energy" id="energy">
								<option value="0" selected="selected">Choisissez</option>
								<%
									if ( energyMap != null )
									{
										for ( Integer energyId : energyMap.keySet() )
										{
								%>
									<option value="<%= energyId %>"><%= energyMap.get(energyId) %></option>
								<%
										}
									}
								%>
							</select>
						</div>
						<div id="defaultFormEntry">
							<label for="is_automatic">Boite de vitesse :</label>
							<select name="is_automatic" id="is_automatic">
								<option value="2" selected="selected">Choisissez</option>
								<option value="0">Manuelle</option>
								<option value="1">Automatique</option>
							</select>
						</div>
						<div id="defaultFormEntry">	
							<label for="price">Prix :</label>
							<input type="text" name="price" id="price" />&nbsp;euros
						</div>
						<div id="defaultFormEntry">	
							<label for="description">Description du véhicule :</label>
							<TEXTAREA cols="80" rows="6" name="description" id="description" maxlength="500"></TEXTAREA>
						</div>
						<div id="defaultFormEntry">	
							<label for="file">Photo (1 Mo max, JPG) :</label>
							<input type="file" name="file" id="file">
						</div>
						<div id="defaultFormEntry">
							<p class="center">
							<input type="submit" value="Publier" name="post"/>
							</p>
						</div>
					</form>
				</div>
			</div>
			
			<jsp:directive.include file="footer.jsp"/>
        </div>
    </body>
</html>